package GUI;

import java.io.IOException;
import java.net.Inet4Address;
import java.net.UnknownHostException;

import javax.swing.JFrame;
import server.OBL_Server;
import java.awt.Dimension;

import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;

import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Button;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.ImageIcon;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JTextArea;

import ocsf.server.ConnectionToClient;

import java.awt.GridLayout;
import javax.swing.UIManager;

public class Server_GUI extends JFrame {
	
	public static int MAIN_WIDTH = 643;
	public static int MAIN_HEIGHT = 465;
	
	private JLabel statusLabel;
	
	private OBL_Server  server;	
	private JTextField passText;
	
	private JTextField userText;
	
	private JPanel topPanel;
	private JPanel menuPanel;
	private JPanel mainPanel;
	
	public JTextArea users;
	public JTextArea log;
	public boolean displayLoggedUsers = false;				//if displaying Logged users = true, Log = false
	
	public Server_GUI(){
		
		server = new OBL_Server(OBL_Server.DEFAULT_PORT,this);
		try {
			server.listen();
		} catch (IOException e) {}
		
		
		ImageIcon LOGOImageIcon = new ImageIcon(getClass().getClassLoader().getResource("resource/LOGO.jpg"));
		ImageIcon LOGO = new ImageIcon (LOGOImageIcon.getImage().getScaledInstance(149, 110, java.awt.Image.SCALE_SMOOTH));
		
		ImageIcon TOPPANELImageIcon = new ImageIcon(getClass().getClassLoader().getResource("resource/TOP.JPG"));
		ImageIcon TOPPANEL = new ImageIcon (TOPPANELImageIcon.getImage().getScaledInstance(650, 110, java.awt.Image.SCALE_SMOOTH));
		
		ImagePanel pnl = new ImagePanel(TOPPANEL.getImage());
		
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setMinimumSize(new Dimension(800,600));
		this.setLocation(200, 80);
		getContentPane().setLayout(null);
		
		topPanel = new JPanel();
		topPanel.setBackground(Color.WHITE);
		topPanel.setBounds(0, 0, 794, 106);
		getContentPane().add(topPanel);
		topPanel.setLayout(null);
		
		JLabel LBLLogo = new JLabel("");
		LBLLogo.setBounds(645, 0, 149, 108);
		topPanel.add(LBLLogo);
		LBLLogo.setIcon(LOGO);
		
		JLabel lblNewLabel = new JLabel("OBL Server");
		lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 18));
		lblNewLabel.setBounds(29, 11, 117, 23);
		topPanel.add(lblNewLabel);
		
		String ip = null;
		try {
			ip = Inet4Address.getLocalHost().getHostAddress().toString();
		} catch (UnknownHostException e1) {}
		
		JLabel ipLabel = new JLabel(ip);
		ipLabel.setFont(new Font("Tahoma", Font.BOLD, 13));
		ipLabel.setBounds(162, 15, 117, 18);
		topPanel.add(ipLabel);
		
		statusLabel = new JLabel("");
		statusLabel.setBounds(31, 68, 179, 14);
		topPanel.add(statusLabel);
		
		menuPanel = new JPanel();
		menuPanel.setBackground(Color.WHITE);
		menuPanel.setBounds(641, 107, 153, 465);
		getContentPane().add(menuPanel);
		menuPanel.setLayout(null);
		
		Button button = new Button("Logged Users");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				DisplayLoggedUsers();
			}
		});
		button.setBounds(10, 10, 133, 55);
		menuPanel.add(button);
		
		Button button_1 = new Button("Log");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				DisplayLog();
			}
		});
		button_1.setBounds(10, 77, 133, 55);
		menuPanel.add(button_1);
		
		mainPanel = new JPanel();
		mainPanel.setBackground(Color.WHITE);
		mainPanel.setBounds(0, 107, 643, 465);
		mainPanel.setLayout(new GridLayout(0, 1, 0, 0));
		getContentPane().add(mainPanel);
		
		final JPanel panel_3 = new JPanel();
		panel_3.setBackground(UIManager.getColor("inactiveCaption"));
		mainPanel.add(panel_3);
		panel_3.setLayout(null);
		
		JLabel lblNewLabel_1 = new JLabel("DB Password:");
		lblNewLabel_1.setBounds(9, 42, 137, 14);
		panel_3.add(lblNewLabel_1);
		
		passText = new JTextField();
		passText.setBounds(156, 39, 113, 20);
		panel_3.add(passText);
		passText.setColumns(10);
		
		JButton btnReady = new JButton("Ready");
		btnReady.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				statusLabel.setText(server.InitDB(userText.getText(),passText.getText()));
				if (statusLabel.getText().equals("Server up!")){
					panel_3.setVisible(false);
					statusLabel.setForeground(new Color(155));
				}else{
					statusLabel.setForeground(Color.red);
				}
			}
		});
		btnReady.setBounds(279, 27, 89, 23);
		panel_3.add(btnReady);
		
		JLabel lblDbUser = new JLabel("DB User:");
		lblDbUser.setBounds(9, 14, 137, 14);
		panel_3.add(lblDbUser);
		
		userText = new JTextField();
		userText.setBounds(156, 11, 113, 20);
		panel_3.add(userText);
		userText.setColumns(10);
		
		this.setTitle("OBL Server - " + ip);
		this.pack();
		this.setVisible(true);
		
		users = new JTextArea("");
		users.setEditable(false);
		users.setSize(MAIN_WIDTH, MAIN_HEIGHT);
		
		log = new JTextArea("");
		log.setEditable(false);
		log.setSize(MAIN_WIDTH, MAIN_HEIGHT);
		
		topPanel.add(pnl);
		
		//server.InitDB("root","root");

	}
	
	
	public void DisplayLog(){
		mainPanel.removeAll();
		

		log.setText(server.logger);
		
		JScrollPane scroller = new JScrollPane(log,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		scroller.setSize(MAIN_WIDTH, MAIN_HEIGHT);
		
		mainPanel.add(scroller);
		mainPanel.revalidate();
		displayLoggedUsers = false;
		
	}
	
	
	public void DisplayLoggedUsers(){
		mainPanel.removeAll();
		
		
		this.users.setText("");

		Thread clients[] = server.getClientConnections();
		for(Thread client : clients){
			ConnectionToClient clientConnection = (ConnectionToClient)client;
			if (!users.getText().equals("")) users.setText(users.getText() + "\n" + clientConnection.getInetAddress().toString().replace("/", "")
					+ " - " + (String)clientConnection.getInfo("UID"));
			else users.setText(clientConnection.getInetAddress().toString().replace("/", "") + " - " + (String)clientConnection.getInfo("UID"));
		}

		JScrollPane scroller = new JScrollPane(users,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		mainPanel.add(scroller);
		
		mainPanel.revalidate();
		displayLoggedUsers = true;
		
	}

	@SuppressWarnings("unused")
	public static void main (String[] args ){
		Server_GUI server_gui = new Server_GUI();
	}
	
	
	class ImagePanel extends JPanel {

		  private Image image;

		  public ImagePanel(Image img) {
		    this.image= img;
		    Dimension size = new Dimension(image.getWidth(null), image.getHeight(null));
		    setPreferredSize(size);
		    setMinimumSize(size);
		    setMaximumSize(size);
		    setSize(size);
		    setLayout(null);
		  }

		  public void paintComponent(Graphics g) {
		    g.drawImage(image, 0, 0, null);
		  }

	}
	
}


